<?php

  $feed = $feed['Feed'];
	
  if (strtolower($format) == 'json') {
		
		$jea = array();
		
		foreach ($entries as $entry) {
			$e = $entry['Entry'];
			$je = new stdClass();
			
			$je->link = $e['link'];
			$je->id = $e['id'];
			$je->title = $e['title'];
			$je->pub_date = $e['pub_date'];
			$je->author = $e['author'];
			$je->description = preg_replace('/<script.+?(<\/script>|$)/is', '<span class="error">[ILLEGAL SCRIPT EMBED!!]</span>', $e['description']);
			$je->description = preg_replace('/(http:\/\/t.co\/[A-Za-z0-9]{8})/is', '<a href="$1" target="_blank">$1</a>', $je->description);
			$je->read = $e['read'];
			$je->guid = $e['guid'];
			$je->feed_id = $e['feed_id'];
			
			array_push($jea, $je);
		}
	
	  $result = new stdClass();
		$result->feed = new stdClass();
		$result->feed->title = $feed['title'];
		$result->feed->html = $feed['html'];
		$result->entries = $jea;
	
		print json_encode($result);
		
	} else {
?>

<h1 class="feedTitle"><a href="<?php echo $feed['html']; ?>" target="_blank"><?php echo $feed['title']; ?></a></h1>


<?php

		if (count($entries) < 1) {
		 ?>
			<ul class="feedControls">
				<li><?php echo $html->link('Mark All As Read', '#', array('id'=>'markAllRead')); ?></li>
				<li><?php echo $html->link('Show Read Items', '#', array('id'=>'toggleShowRead')); ?></li>
			</ul>
			<div id="entries">
				<ul class="feedEntries">
					<li class="feedEntry">
						<!-- <div class="notice">There are no unread entries</div> -->
						
						<h1 class="greeting">Welcome to Dependence!</h1>
						<div>
							Your subscriptions are to the right, please let us know if you encounter any bugs that last (long term).  We're still actively hacking on the code so things might break occasionally.
						</div>
					</li>
				</ul>
			</div>
		 <?php
		 
		} else {
?>

<!--
  DEBUG INFO

  <?php echo $elapsed; ?> seconds elapsed in feed retrieval.<br/>
  <?php echo microtime(true) - $controller_start; ?> seconds elapsed in controller actions.
	
-->
<ul class="feedControls">
	<li><?php echo $html->link('Mark All As Read', '#', array('id'=>'markAllRead')); ?></li>
	<li><?php echo $html->link('Show Read Items', '#', array('id'=>'toggleShowRead')); ?></li>
</ul>

<div id="entries">
	<ul class="feedEntries">
	<?php
	foreach ($entries as $entry) {
		$e = $entry['Entry'];
		
		/*<?php echo date('l jS \of F Y h:i:s A', $e['pub_date']); ?>*/
		
		?>
		<li class="feedEntry" id="entryID-<?php echo $e['id']; ?>">
		  
			<h3><?php echo $html->link($e['title'], $e['link'], array('target'=>'_blank')); ?></h3>
			<ul class="metaData">
				<li><strong>Date:</strong> <?php echo date('l jS \of F Y h:i:s A', $e['pub_date']); ?></li>
				<?php
				if (strlen($e['author']) > 0) {
					?><li><strong>Author:</strong> <?php echo $e['author']; ?></li>
						<?php
				}
				?>
			</ul>
			<div class="feedBody">
				<?php
					
					// TO-DO: Sanitize the shit out of the description
				
				  $desc = $e['description'];
					$desc = preg_replace('/<script.+?(<\/script>|$)/is', '<span class="error">[ILLEGAL SCRIPT EMBED!!]</span>', $desc);
					$desc = preg_replace('/(http:\/\/t.co\/[A-Za-z0-9]{8})/is', '<a href="$1" target="_blank">$1</a>', $desc);
				
				?>
				<?php echo $desc; ?>
			</div>
			<div class="entryControls">
				<input type="checkbox" class="keepUnread" val="<?php echo $e['id']; ?>" /> <a href="#">Keep Unread</a>  &nbsp;
				<span class="icon icon-emailFriend"></span> <a href="#" class="emailFriend">Email to a friend</a>  &nbsp;
				<span class="icon icon-share"></span> <a href="#" class="share">Share</a>  &nbsp;
				<span class="icon icon-shareWithNote"> </span> <a href="#" class="shareWithNote">Share with Note</a>
			</div>
		</li><!-- /.feedEntry -->
		<?php
	}
	?>
	</ul><!-- /.feedEntries -->
</div>

<?php
		} // count(entries) > 0
	} // format?
	
?>